﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DaoYou.Web.Guider.Account;
using m = RenChangYou.Model;
using b = RenChangYou.BLL;
using RenChangYou.Common;
using RenChangYou.Model;

namespace DaoYou.Web
{
    public partial class Detail : PageBase
    {
        protected string GuiderUserName
        {
            get
            {
                return Request.QueryString["username"] == null ? string.Empty : Request.QueryString["username"];
            }
        }

        protected RenChangYou.Model.DY_GuiderInfo GuiderInfo = null;

        protected string Introduction = null;

        protected RenChangYou.Model.DY_User UserInfo = null;

        protected List<RenChangYou.Model.DY_GuidePhoto> GuidePhotoList = null;

        protected List<RenChangYou.Model.DY_GuideProduct> GuideProductList = null;

        protected List<m.DY_Comment> CommentList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.GuiderUserName))
            {
                {
                    RenChangYou.BLL.DY_GuiderInfo bll = new RenChangYou.BLL.DY_GuiderInfo();

                    GuiderInfo = bll.GetModelByCache(this.GuiderUserName);

                    if (GuiderInfo == null)
                    {
                        Common.AlertMessageAndRedirect("对不起，您访问的导游信息有误！", Page.ResolveUrl("~/Default.aspx"));
                    }
                }
                {
                    RenChangYou.BLL.DY_GuideIntroduction bll = new RenChangYou.BLL.DY_GuideIntroduction();

                    var model = bll.GetModelList(string.Format(" UserName = '{0}' and IsInUse = 1 ", this.GuiderUserName)).FirstOrDefault();

                    if (model != null)
                    {
                        Introduction = model.Introduction;
                    }
                }
                {
                    RenChangYou.BLL.DY_User bll = new RenChangYou.BLL.DY_User();

                    UserInfo = bll.GetModelByCache(GuiderUserName);
                }
                {
                    RenChangYou.BLL.DY_GuidePhoto bll = new RenChangYou.BLL.DY_GuidePhoto();

                    GuidePhotoList = bll.GetModelList(string.Format(" UserName = '{0}' ", GuiderUserName));
                }
                {
                    RenChangYou.BLL.DY_GuideProduct bll = new RenChangYou.BLL.DY_GuideProduct();

                    GuideProductList = bll.GetModelList(string.Format(" UserName = '{0}' ", GuiderUserName));
                }
                {
                    b.DY_Comment bll = new b.DY_Comment();
                    CommentList = bll.GetModelList(string.Format(" GuideName = '{0}' ", GuiderUserName));
                }
                {
                    b.DY_OrderDetail bll = new b.DY_OrderDetail();
                    string where = string.Format(" GuideUserName = '{0}' ", this.GuiderUserName);

                    //bll.GetModelList();
                }
            }
        }

        //立即预订
        protected void btn_booking_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = DateTime.Now.Ticks.ToString();
                int dailyPrice = 100;
                int days = 0;

                if (PageValidate.IsDateTime(this.txt_startDate.Value) && PageValidate.IsDateTime(this.txt_endDate.Value))
                {
                    days = (Convert.ToDateTime(this.txt_endDate.Value) - Convert.ToDateTime(this.txt_startDate.Value)).Days;
                }

                //try
                //{
                //    b.DY_GuideProduct productBll = new b.DY_GuideProduct();
                //    dailyPrice = Convert.ToInt32(productBll.GetModelList(string.Format(" UserName = '{0}' ", GuiderUserName))[0].PriceA);
                //}
                //catch {
                //    dailyPrice = 100;
                //}

                b.DY_GuiderPrice priceBll = new b.DY_GuiderPrice();
                m.DY_GuiderPrice priceModel = priceBll.GetModelByCache(GuiderUserName);
                dailyPrice = priceModel == null ? 100 : priceModel.SalePrice;

                m.DY_Order ordermod = new m.DY_Order();
                ordermod.OrderNo = orderNo;
                ordermod.CustomerUserName = Account;
                ordermod.CreatedTime = DateTime.Now;
                ordermod.TotalPrice = dailyPrice * days;
                ordermod.OrderStatus = OrderStatus.A;

                m.DY_OrderDetail ordDetailmod = new m.DY_OrderDetail();
                ordDetailmod.OrderNo = orderNo;
                ordDetailmod.GuideUserName = GuiderUserName;
                ordDetailmod.GuideProductId = -1;  //默认产品ID1
                ordDetailmod.Price = dailyPrice;
                ordDetailmod.Remark = this.txtRemark.Text;
                ordDetailmod.StartDate = Convert.ToDateTime(this.txt_startDate.Value);
                ordDetailmod.EndDate = Convert.ToDateTime(this.txt_endDate.Value);
                ordDetailmod.ACity = this.txtACity.Value;
                ordDetailmod.DCity = this.txtDCity.Value;
                ordDetailmod.PersonNumber = Convert.ToInt32(this.txtPersonNumber.Value);

                b.DY_Order orderbll = new b.DY_Order();
                orderbll.Add(ordermod);
                b.DY_OrderDetail orderDetbll = new b.DY_OrderDetail();
                orderDetbll.Add(ordDetailmod);

                b.DY_OrderContact orderContact = new b.DY_OrderContact();
                orderContact.Add(new m.DY_OrderContact()
                {
                    AddressA = string.Empty,
                    AddressB = string.Empty,
                    ContactWayA = this.txtContactWayA.Value,
                    ContactWayB = this.txtContactWayB.Value,
                    OrderNo = orderNo,
                    PersonA = this.txtPersonA.Value,
                    PersonB = this.txtPersonB.Value
                });

                string payurl = Page.ResolveUrl(string.Format("~/Pay.aspx?OrderNo={0}&Price={1}&UserName={2}", orderNo, dailyPrice * days, base.Account));
                Common.AlertMessageAndRedirect("订单提交成功，请去支付", payurl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        /// <summary>
        /// 评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_comment_Click(object sender, EventArgs e)
        {
            m.DY_Comment commnetmod = new m.DY_Comment();
            commnetmod.GuideName = GuiderUserName;
            commnetmod.userName = base.Account;
            commnetmod.rank = Convert.ToInt32(this.ddlScore.SelectedValue);
            commnetmod.rankContent = this.txtComment.Value.Trim();
            commnetmod.CreatedTime = DateTime.Now;

            b.DY_Comment commentbll = new b.DY_Comment();
            int result = commentbll.Add(commnetmod);
            if (result > 0)
            {
                Common.AlertMessageAndRedirect("评论成功！", Page.ResolveUrl("~/Detail.aspx?username=" + this.GuiderUserName));
            }
            else
            {
                Common.AlertMessage("评论失败！");
            }

        }

        protected string UserHeadImage(string username)
        {
            RenChangYou.BLL.DY_User bll = new b.DY_User();
            RenChangYou.Model.DY_User model = bll.GetModelByCache(username);

            if (model != null && !string.IsNullOrWhiteSpace(model.HeadImageName))
            {
                return Page.ResolveUrl(string.Format("~/upload/{0}/{1}", username, model.HeadImageName));
            }

            return string.Empty;
        }
    }
}